﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace Please_starve.Common.Managers
{
    /// <summary>
    /// Resource manager for texture2D and spritefonts. So they dont have to be loaded twice.
    /// 
    /// Ex of use:
    /// Texture2D texture = Resources.GetTexture("AssetName");
    /// </summary>
    public static class Resources
    {
        private static ContentManager content = GameServices.GetService<ContentManager>();

        private static Dictionary<String, Texture2D> textures = new Dictionary<string, Texture2D>();
        private static Dictionary<String, SpriteFont> fonts = new Dictionary<string, SpriteFont>();

        public static Texture2D GetTexture(string filePath)
        {
            Texture2D texture = null;
            textures.TryGetValue(filePath, out texture);

            if (texture == null)
            {
                texture = content.Load<Texture2D>(filePath);
                textures.Add(filePath, texture);
                return texture;
            }
            else
            {
                return texture;
            }
        }

        public static SpriteFont GetFont(string filePath)
        {
            SpriteFont font = null;

            fonts.TryGetValue(filePath, out font);

            if (font == null)
            {
                font = content.Load<SpriteFont>(filePath);
                fonts.Add(filePath, font);
                return font;
            }
            else
            {
                return font;
            }
        }
    }
}
